$(document).ready(function(){
	 $('.table-sort').dataTable(pageStting());
	 $('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
	 var date=new Date();
	 $('#stratDate,#endDate').val(date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate())
});
$(window).resize(function(){
	$(".table-sort").css('width','100%');
});
//从服务器分批取数据
pageStting = function(){
		var optsortable = {
					"bPaginate":true,
					"stateSave":true,
					"bProcessing":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/specialManager/list',
					"sPaginationType":'full_numbers',
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [
										{               
											   "bVisible":false,
											   "aTargets":[0]
										},
										{               
											   "bVisible":false,
											   "aTargets":[1]
										}
				                      ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($.trim($("#stratDate").val())!=''){
								searchConditions.startDate=$.trim($("#stratDate").val());
							}
							if($.trim($("#endDate").val())!=''){
								searchConditions.endDate=$.trim($("#endDate").val());
							}
							if($.trim($("#userName").val())!=''){
								searchConditions.topic=$.trim($("#userName").val());
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递  
						        success: function(result) {
						        	if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										//json重构数组对象 
										$.each(data,function(i,val){
											aaData[i]=[]
											aaData[i].push(val.publishId);
											aaData[i].push(val.ssid);
											aaData[i].push(getContent(val.topic));
											aaData[i].push(getContent(val.publishOrg));
											aaData[i].push(getAirPort(val.flight));
											aaData[i].push(getReceivePart(val.receiveList)==""?"-":getReceivePart(val.receiveList));
											aaData[i].push(getReceivePerson(val.receiveList)==""?"-":getReceivePerson(val.receiveList));
											aaData[i].push(getTime(val.publishTime));
											aaData[i].push(val.level);
											aaData[i].push(getStatus(val.sStatus));
											//aaData[i].push(val.null_n);
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}
function getReceivePart(obj){
	var getReceivePartHtml="";
	$.each(obj,function(i,n){
		if(n.type=="0"){
			getReceivePartHtml+=n.receiveOrg+"<br>";
		}
	})
	return getReceivePartHtml
}
function getReceivePerson(obj){
	var getReceivePersonHtml="";
	$.each(obj,function(i,n){
		if(n.type=="2"){
			getReceivePersonHtml+=specialOrgName(n.receiveOrg)+"-"+n.receiveId+"<br>";
		}
	})
	return getReceivePersonHtml
}
function getTime(t){
	 var date=new Date(t);
	 return date.getFullYear()+"-"+changeTime((date.getMonth()+1))+"-"+changeTime(date.getDate())+"&nbsp;"+changeTime(date.getHours())+":"+changeTime(date.getMinutes());
}
function changeTime(obj){
return obj<10?"0"+obj:obj;
}
function specialOrgName(obj){
	return obj==null?"":obj;
}
/*function getStatus(obj){
	 if(obj=="1"){//1：接收者；2：转发者；3：自发起，可强制结束或撤销；4：已完成
		 	return "已回执";
		 }else if(obj=="2"){
			 return "已回复";
		 }else if(obj=="10"){
			 return "已处理";
		 }else if(obj=="11"){
			 return "已撤消";
		 }else if(obj=="12"){
			 return "已强制结束";
		 }else if(obj=="0"){
			 return "未回执";
		 }else return '/'
}*/
function getStatus(obj){
	 if(obj=="1" || obj=="2"){//1：接收者；2：转发者；3：自发起，可强制结束或撤销；4：已完成
		 	return "<span class='c-black'>处理中</span>";
		 }else if(obj=="12" || obj=="11" || obj=="10"){
			 return "<span class='c-999'>已完成</span>";
		 }else if(obj=="0"){
			 return "<span class='c-red'>待处理</span>";
		 }else return '-'
}
doSearch = function(){
   var seachValue={};
   $('.table-sort').DataTable().search(seachValue).draw();
}	
doReset= function(){
	$('.searchCommon').find('input,select').val('');
}
function getContent(obj){
	if(obj.length<=30){
		return "<span>"+obj+"</span>";
	}else return "<span>"+obj.substring(0,30)+"...</span>";
}
function getAirPort(obj){
	if(obj!=null){
		if(obj.length>5){
			return obj.slice(0,5)+"...";
		}else return obj;
	}else return "-";
}